package info.erge.sort;

public class SelectionSort {
    private SelectionSort() {
    }

    public static <E extends Comparable<E>> void sort(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j].compareTo(arr[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    public static <E extends Comparable<E>> void sort2(E[] arr) {
        for (int i = arr.length - 1; i >= 0 ; i--) {
            int maxIndex = i;
            for (int j = i; j > 0; j--) {
                if (arr[j].compareTo(arr[maxIndex]) > 0) {
                    maxIndex = j;
                }
            }
            swap(arr, maxIndex, i);
        }
    }

    public static <E> void swap(E[] arr, int i, int j) {
        E t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
        Integer[] arr = { 1, 5, 6, 3, 5 };
        SelectionSort.sort2(arr);
        for (int s : arr) {
        System.out.println(s);
        }
        // Student[] students = {
        // new Student((short) 25),
        // new Student((short) 18),
        // new Student((short) 22)
        // };
        // SelectionSort.sort(students);
        // for (Student student : students) {
        // System.out.println(student);
        // }
        // int[] dataSize = { 10000, 100000 };
        // for (int n : dataSize) {
        //     Integer[] arr = ArrayGenerator.generateRandomdArray(n, n);
        //     SortingHelper.sortTest("SelectionSort", arr);
        // }

    }
}
